package eip.chapter4.invalidmessage.mule;

import java.io.StringReader;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.JiBXException;
import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;

import eip.chapter4.invalidmessage.HotelSearchMessage;

/**
 * This transformer transforms the incoming XML message into
 * a HotelSearch message. For this transformer we use JiBX.
 * 
 * The mapping file used is:
 *  resource/eip/chapter4/invalidmessage/xml/HotelMapping.xml
 * 
 * @author nl24167
 *
 */
public class HotelSearchTransformer extends AbstractTransformer {

	/** serializable class */
	private static final long serialVersionUID = 2743559698371523938L;

	/**
	 * The source is a string that represents our searchMessage.
	 */
	@Override
	protected Object doTransform(Object src, String encoding) throws TransformerException {
		String xmlInput = (String) src;
		Object result = null;
	    try {
			IBindingFactory bfact = BindingDirectory.getFactory(HotelSearchMessage.class);
			result = (HotelSearchMessage) bfact.createUnmarshallingContext().
						unmarshalDocument(new StringReader(xmlInput),null);
		} catch (JiBXException e) {
			throw new TransformerException(this,e);
		}
		return result;
	}
}
